WOLF core
WOLF: A Modular Estimation Framework for Robotics
XXXXXXXXXXXXXXXXXXXXXXXXvoid(0)" frameborder="0" name="MSearchResults" id="MSearchResults">
factory.h
Go to the documentation of this file.
1// WOLF - Copyright (C) 2020-2026
2// Institut de Robòtica i Informàtica Industrial, CSIC-UPC.
3// Authors: Joan Solà Ortega (jsola@iri.upc.edu) and
4// Joan Vallvé Navarro (jvallve@iri.upc.edu)
5// All rights reserved.
6//
7// This file is part of WOLF: http://www.iri.upc.edu/wolf
8// WOLF is free software: you can redistribute it and/or modify
9// it under the terms of the GNU General Public License version 3
10// as published by the Free Software Foundation.
11//
12// This program is distributed in the hope that it will be useful,
13// but WITHOUT ANY WARRANTY; without even the implied warranty of
14// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15// GNU General Public License for more details.
16//
17// You should have received a copy of the GNU General Public License
18// along with this program. If not, see <http://www.gnu.org/licenses/>.
19#pragma once
20
21// wolf
22#include "core/common/wolf.h"
23#include "core/utils/unused.h"
24
25// std
26#include <string>
27#include <map>
28#include <list>
29#include <iostream>
30#include <iomanip>
31
32namespace wolf
33{
258template <class TypeBase, typename... TypeInput>
260{
261 public:
262 // example of creator callback (see typedefs below)
263 typedef TypeBase (*CreatorCallback)(TypeInput... _input);
264
265 private:
266 typedef std::map<std::string, CreatorCallback> CallbackMap;
267
268 public:
269 static bool registerCreator(const std::string& _type, CreatorCallback createFn);
270 static bool unregisterCreator(const std::string& _type);
271 static bool isCreatorRegistered(const std::string& _type);
272 static TypeBase create(const std::string& _type, TypeInput... _input);
273 std::string getClass() const;
274 static void printAddress();
275 static void printCallbacks();
276 static std::list<std::string> getRegisteredKeys();
277
278 private:
279 CallbackMap callbacks_;
280
281 // Singleton ---------------------------------------------------
282 // This class is a singleton. The code below guarantees this.
283 // See: http://stackoverflow.com/questions/1008019/c-singleton-design-pattern
284 private:
285 static Factory& get();
286
287 public:
288 Factory(const Factory&) = delete;
289 void operator=(Factory const&) = delete;
290
291 private:
292 Factory();
293 ~Factory();
294};
295
296template <class TypeBase, typename... TypeInput>
298{
299 // std::cout << " Factory constructor " << this->getClass() << std::endl;
300}
301
302template <class TypeBase, typename... TypeInput>
303inline Factory<TypeBase, TypeInput...>::~Factory()
304{
305 // std::cout << " Factory destructor " << this->getClass() << std::endl;}
306}
307
308template <class TypeBase, typename... TypeInput>
309inline bool Factory<TypeBase, TypeInput...>::registerCreator(const std::string& _type, CreatorCallback createFn)
310{
311 if (get().callbacks_.count(_type) == 1 and get().callbacks_.at(_type) != createFn)
312 throw std::runtime_error(get().getClass() + " : For the type \"" + _type +
313 "\": Trying to register a creator but there is a different creator registered.");
314
315 bool reg = get().callbacks_.insert(typename CallbackMap::value_type(_type, createFn)).second;
316
317 if (reg) std::cout << std::setw(31) << std::left << get().getClass() << " <-- registered " << _type << std::endl;
318
319 return reg;
320}
321
322template <class TypeBase, typename... TypeInput>
323inline bool Factory<TypeBase, TypeInput...>::unregisterCreator(const std::string& _type)
324{
325 return get().callbacks_.erase(_type) == 1;
326}
327
328template <class TypeBase, typename... TypeInput>
329inline bool Factory<TypeBase, TypeInput...>::isCreatorRegistered(const std::string& _type)
330{
331 return get().callbacks_.count(_type) == 1;
332}
333
334template <class TypeBase, typename... TypeInput>
335inline TypeBase Factory<TypeBase, TypeInput...>::create(const std::string& _type, TypeInput... _input)
336{
337 typename CallbackMap::const_iterator creator_callback_it = get().callbacks_.find(_type);
338
339 if (creator_callback_it == get().callbacks_.end())
340 // not found
341 throw std::runtime_error(get().getClass() + " : Unknown type \"" + _type +
342 "\". Possibly you tried to use an unregistered creator.");
343
344 // Invoke the creation function
345 return (creator_callback_it->second)(std::forward<TypeInput>(_input)...);
346}
347
348template <class TypeBase, typename... TypeInput>
349inline Factory<TypeBase, TypeInput...>& Factory<TypeBase, TypeInput...>::get()
350{
351 static Factory<TypeBase, TypeInput...> instance_;
352 return instance_;
353}
354
355template <class TypeBase, typename... TypeInput>
357{
358 return "Factory<class TypeBase>";
359}
360
361template <class TypeBase, typename... TypeInput>
363{
364 std::cout << get().getClass() << " address: " << &get() << std::endl;
365}
366
367template <class TypeBase, typename... TypeInput>
369{
370 std::cout << get().getClass() << " callbacks size: " << get().callbacks_.size() << std::endl;
371 for (auto cb : get().callbacks_)
372 {
373 std::cout << "\t" << cb.first << std::endl;
374 }
375}
376
377template <class TypeBase, typename... TypeInput>
379{
380 std::list<std::string> keys;
381 for (auto cb : get().callbacks_)
382 {
383 keys.push_back(cb.first);
384 }
385 return keys;
386}
387
388} // namespace wolf
Singleton template factory.
Definition: factory.h:260
static void printCallbacks()
Definition: factory.h:368
static bool registerCreator(const std::string &_type, CreatorCallback createFn)
Definition: factory.h:309
static TypeBase create(const std::string &_type, TypeInput... _input)
Definition: factory.h:335
static std::list< std::string > getRegisteredKeys()
Definition: factory.h:378
TypeBase(* CreatorCallback)(TypeInput... _input)
Definition: factory.h:263
static bool isCreatorRegistered(const std::string &_type)
Definition: factory.h:329
void operator=(Factory const &)=delete
std::string getClass() const
Definition: factory.h:356
Factory(const Factory &)=delete
static void printAddress()
Definition: factory.h:362
static bool unregisterCreator(const std::string &_type)
Definition: factory.h:323
Definition: capture_base.h:23